package cate.game.role.farm.dailyfb;

import cate.common.table.d.GDDailyFb;
import cate.common.table.farm.dailyfb.DailyFbCombatRow;
import cate.common.table.farm.dailyfb.DailyFbTypeRow;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.farm.dailyfb.msg.DailyFbOpenResp;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;

import java.util.List;

public class DailyFb extends RoleDependent {
    @NoteField(value = "副本类型")
    public int type;

    @NoteField(value = "已开启的关数")
    public int combatNumOpen = 1;

    @NoteField(value = "已通关的关数")
    public int combatNumPass = 0;

    @NoteField(value = "今日de挑战次数记录")
    public DailyFbPlayTimes playTimes;

    @NoteField(value = "碎钻扫荡标记")
    public boolean spendMark;

    @Transient
    @NoteField(value = "副本是否开启")
    public boolean open;

    public DailyFb() {
    }

    public DailyFb(int type) {
        this.type = type;
    }

    public void setCombatNumPass(int value) {
        if (value > combatNumPass) {
            combatNumPass = value;
        }
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (playTimes == null)
            playTimes = new DailyFbPlayTimes();
        playTimes.initialize();
    }


    /**
     * 更新数据（用于检查是否开启新的副本）
     */
    public void update() {
        int shouldOpen = 0; // 应该开启的关数
        List<DailyFbCombatRow> tpls = role.getGame().table.dailyFb.combat.listByType(type);
        List<Integer> types = role.getGame().table.dailyFb.type.getOpenList();
        if (tpls == null || !types.contains(type)) {
            logger.error("日常副本配置错误");
            return;
        }

        for (DailyFbCombatRow tpl : tpls) {
            if (role.getBase().level >= tpl.levelReq && role.getBase().power >= tpl.powerReq) {
                if (tpl.combatNum > shouldOpen) {
                    shouldOpen = tpl.combatNum;
                }
            }
        }
        if (shouldOpen > combatNumOpen) {
            combatNumOpen = shouldOpen;
            role.sendNow(new DailyFbOpenResp(type, combatNumOpen));
        }
    }

    /**
     * 检测开启
     *
     * @return 该子副本是否开启
     */
    public boolean checkOpen() {
        DailyFbTypeRow row = getTypeRow(this.type);
        if (row == null)
            return false;
        this.open = (row.dayRequire <= role.getGame().getOpenDay())
                & (row.levelRequire <= role.getBase().level)
                & (row.open == GDDailyFb.FB_OPEN);

        return this.open;
    }

    private DailyFbTypeRow getTypeRow(int type) {
        if (role.getGame().table.dailyFb != null) {
            return role.getGame().table.dailyFb.type.get(type);
        }
        return null;
    }


    /**
     * 重置数据（因为跨天）
     */
    public void onDaySpan() {
        if (playTimes != null) {
            role.getRes().findBack.triggerByFuncId(GDDailyFb.Type.toFuncId(type));
            playTimes.reset();
        }
    }
}
